<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="求救内容" prop="cryContent">
        <el-input v-model="queryParams.cryContent"
                  placeholder="请输入求救内容"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="求救时间" prop="cryTime">
        <el-date-picker
          v-model="cryTime"
          type="datetimerange"
          align="right"
          size="small"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['frontierCommand:help:add']"-->
      <!--        >新增-->
      <!--        </el-button>-->
      <!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['frontierCommand:help:edit']"-->
<!--        >修改-->
<!--        </el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['frontierCommand:help:remove']"
        >批量删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['frontierCommand:help:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="helpList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主建id" align="center" prop="id" v-if="false"/>
      <el-table-column label="求救时间" align="center" prop="cryTime" width="180">
        <!--        <template slot-scope="scope">-->
        <!--          <span>{{ parseTime(scope.row.cryTime, '{y}-{m}-{d}') }}</span>-->
        <!--        </template>-->
      </el-table-column>
      <el-table-column label="求救人" align="center" prop="cryName"/>
      <el-table-column label="求救人账号" align="center" prop="createBy"/>
      <el-table-column label="求救内容" align="center" prop="cryContent"/>
      <el-table-column label="X坐标" align="center" prop="x"/>
      <el-table-column label="Y坐标" align="center" prop="y"/>
      <el-table-column label="定位" align="center" prop="isOnline">
        <template slot-scope="scope">
          <el-button v-if="scope.row.isOnline !== null"
                     size="mini"
                     type="text"
                     @click="handleOpenMap(scope.row,1)"
          >定位
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['frontierCommand:help:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['frontierCommand:help:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改求救信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="求救时间" prop="cryTime">
          <el-date-picker clearable size="small"
                          v-model="form.cryTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择求救时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="求救人" prop="cryName">
          <el-input v-model="form.cryName" disabled placeholder="请输入求救人"/>
        </el-form-item>
        <el-form-item label="求救内容">
          <el-input v-model="form.cryContent" type="textarea" placeholder="请输入求救内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="'地图'"
               :fullscreen="true"
               :visible="openMap"
               :close-on-click-modal="false"
               class="dialog_collect"
               @open="handleOpenDialog"
               @close="handleCloseDialog">
      <section class="dialog_body">
        <common-map
          ref="map"
          style="height: calc(100vh - 175px)"
          @map-click="ShowDetail"
          :isTool="false"
          :toolbarSelectVisible="false"
        >
        </common-map>
      </section>
      <user-detail
        :visible='showUserDialog'
        @close="showDetailDialog = -1"
        ref="userDetail">
      </user-detail>
      <section slot="footer" class="dialog-footer">
        <el-button @click="handleCloseDialog">关 闭</el-button>
      </section>
    </el-dialog>
  </div>
</template>

<script>
  import { listHelp, getHelp, delHelp, addHelp, updateHelp, exportHelp } from '@/api/frontierCommand/help'
  import Editor from '@/components/Editor'
  import TyCoordinateConvert from '../../../plugin/map/TyCoordinateConvert'
  import userDetail from '../../onemap/Detail/userDetail'

  export default {
    name: 'Help',
    components: {
      Editor, userDetail
    }, computed: {
      showUserDialog() {
        return this.showDetailDialog === 1
      }
    },
    data() {
      return {
        //地图控制
        map: null,
        showDetailDialog: -1,
        openMap: false,
        rowData: {},
        marker: [],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        names: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 求救信息表格数据
        helpList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          cryContent: undefined,
          orderByColumn: 'cryTime',
          cryTime: undefined,
          isAsc: 'desc'
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        cryTime: '',
        pickerOptions: {
          disabledDate(time) {
            return time.getTime() > new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1)
          },
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }]
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      /** 查询求救信息列表 */
      getList() {
        this.loading = true
        if (this.cryTime !== '') {
          this.queryParams.beginDate = this.cryTime[0]
          this.queryParams.overDate = this.cryTime[1]
        }
        listHelp(this.queryParams).then(response => {
          this.helpList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          cryTime: undefined,
          cryName: undefined,
          cryContent: undefined,
          x: undefined,
          y: undefined,
          verify: undefined,
          issue: undefined,
          delFlag: undefined,
          createById: undefined,
          createBy: undefined,
          createTime: undefined,
          updateById: undefined,
          updateBy: undefined,
          updateTime: undefined
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.queryParams.beginDate = undefined
        this.queryParams.overDate = undefined
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.cryTime = ''
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.names = selection.map(item => item.cryName)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = '添加求救信息'
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const id = row.id || this.ids
        getHelp(id).then(response => {
          this.form = response.data
          this.open = true
          this.title = '修改求救信息'
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateHelp(this.form).then(response => {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            } else {
              addHelp(this.form).then(response => {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids
        const names = row.cryName || this.names
        this.$confirm('是否确认删除求救人为"' + names + '"的数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delHelp(ids)
        }).then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
      },
      /** 打开地图定位*/
      handleOpenMap(row, e) {
        this.openMap = true
        this.rowData = row

      },
      handleOpenDialog() {
        this.$nextTick(() => {
          this.map = this.$refs.map.map
          let gcj = TyCoordinateConvert.bd_decrypt(this.rowData.y, this.rowData.x)
          let wgs84 = TyCoordinateConvert.gcj_decrypt_exact(gcj.lat, gcj.lon)
          this.marker.push({
            coordinates: [wgs84.lon, wgs84.lat],
            id: this.rowData.id,
            title: this.rowData.name,
            target: 'user',
            animate: true,
            stopEvent: true,
            iconUrl: '/image/map/user.png',
            item: this.rowData
          })
          this.$refs.map.showMarkerFeature(this.marker, false, 'user')
          this.$refs.map.map.getMap().getView().animate({
            center: [wgs84.lon, wgs84.lat],
            duration: 500
          })
        })
      },
      ShowDetail(e) {
        this.$refs.userDetail.handleShowCaseInfo(e, this.map)
        this.showDetailDialog = 1
      },
      handleCloseDialog() {
        this.showDetailDialog = -1
        this.marker = []
        this.openMap = false
        this.$forceUpdate()
      },
      /** 导出按钮操作 */
      handleExport() {
        let queryParams1 = { ...this.queryParams }
        queryParams1.pageSize = 99999
        queryParams1.pageNum = 1
        this.$confirm('是否确认导出所有求救信息数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return exportHelp(queryParams1)
        }).then(response => {
          this.download(response.msg)
        })
      }
    }
  }
</script>
